package cn.kailangtu.serialutils.api;

import cn.kailangtu.commontools.Loggers;

public class DIManager {

    private int item1Value  = 0;
    private int item2Value = 0;
    private int item3Value = 0;


    DIMessageListener listener;

    static DIManager instance;
    public static DIManager getInstance(){
        if(instance == null)
        {
            instance = new DIManager();
        }
        return instance;
    }


    private DIManager(){

    }

    public void initListener(DIMessageListener listener){
        this.listener = listener;
    }

    public void onDiMessage(int itemNo,int action)
    {
        //Loggers.errInfo("收到消息:"+itemNo+"|"+action);

        if(listener != null )
        {
            listener.onReceiveAction(itemNo,action,getActionMsg(action));
        }
    }

    private String getActionMsg(int action){
        if(action == 0)
        {
            return "无操作";
        }
        else if(action == 1){
            return "按下";
        }
        else if(action == 2)
        {
            return "释放";
        }
        return "";
    }

    public void newValue(int item1Value,int item2Value,int item3Value)
    {
        if(item1Value != this.item1Value)
        {
            this.item1Value = item1Value;
            onDiMessage(0,item1Value);
        }

        if(item2Value != this.item2Value)
        {
            this.item2Value = item2Value;
            onDiMessage(1,item2Value);
        }

        if(item3Value != this.item3Value)
        {
            this.item3Value = item3Value;
            onDiMessage(2,item3Value);
        }

    }

}
